package com.sllp.C71S3TsllpShop.web;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.SmallType;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.BigTypeBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.BusinessBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.FavoriteBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.ProductBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.SmallTypeBizImpl;
import com.sllp.C71S3TsllpShop.dao.BigTypeMapper;
import com.sllp.C71S3TsllpShop.dao.ProductMapper;

@Controller
public class IndexAction {

	@Resource
	private BigTypeMapper btm;
	
	@Resource
	private ProductMapper pm;
	
	@Resource
	private ProductBizImpl pbiz;
	
	@Resource
	private BigTypeBizImpl btbiz;
	
	@Resource
	private SmallTypeBizImpl stbiz;
	
	@Resource
	private CarBizImpl cbiz;
	
	@Resource
	private FavoriteBizImpl fbiz;
	
	@Resource
	private BusinessBizImpl bbi;
	
//	首页的分页行数
	private final static int PAGE_SIZE=1;
	
	@GetMapping({"/","index","index.do"})
	public String toindex(HttpSession session,
			@SessionAttribute(name="loginUser",required=false) User user,
			@SessionAttribute("btlist") List<List<BigType>> lists,Model m){
		//热销产品显示
		List<Product> list=pbiz.findHotProduct();
		m.addAttribute("hotlist", list);
		
	    //全部商品分类显示
//		List<BigType> source=btm.selectByExample(null);
//		List<List<BigType>> lists=btbiz.findAllBigType(source);
		m.addAttribute("btlist", lists);
		
		//食品生鲜
		List<SmallType> list1=stbiz.findProductByType1();
		m.addAttribute("list1", list1);
		
		//零食饮料
		List<SmallType> list2=stbiz.findProductByType2();
		m.addAttribute("list2", list2);
		
		//个人美妆
		List<SmallType> list3=stbiz.findProductByType3();
		m.addAttribute("list3", list3);
		
		//母婴玩具
		List<SmallType> list4=stbiz.findProductByType4();
		m.addAttribute("list4", list4);
		
		//家居生活
		List<SmallType> list5=stbiz.findProductByType5();
		m.addAttribute("list5", list5);
		
		//数码家电
		List<SmallType> list6=stbiz.findProductByType6();
		m.addAttribute("list6", list6);
		
		//右上角购物车显示
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
		
		m.addAttribute("alist",pm.selectByExampleWithBLOBs(null));
		return "index";
	}
	@GetMapping("findProductIndex")
	public ModelAndView indexSearch(Model m,String name){
		SmallType small=stbiz.findSmallTypeByCondition(name);
		if(small!=null){
			return new ModelAndView("redirect:categorylist?id="+small.getId());
		}else{
			return null;
		}
	}

}
